home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-11-29 | 11.7 KB | 556 lines | [TEXT/KAHL] |
- /******************************************************************************
- GNUStaticPane.c
-
- A superclass of CAbstractText, with functionality similar to CEditPane in static
- text mode, but does
- not make use of textedit, so can handle arbitrary size buffers.
-
- Body of Draw, CalcLineStarts, IndexToLine methods
- Incorporate code originally written by
- Roy Wood 122 Britannia Avenue London, Ontario, Canada N6H 2J5
-
- Brazenly hacked for TCL compatibility by Jonathan Kimmitt.
-
- The many bugs introduced during this process are entirely my fault
-
- */
-
- #include <CDocument.h>
- #include "GNUStaticPane.h"
- #include "CClipboard.h"
- #include "Commands.h"
- #include "TCLUtilities.h"
- #include "Constants.h"
- #include "CScrollPane.h"
- #include "Global.h"
-
- extern CClipboard *gClipboard;
- extern EventRecord gLastMouseUp;
- extern CBureaucrat *gGopher;
-
- void GNUStaticPane::IGNUStaticPane(CView *anEnclosure, CBureaucrat *aSupervisor)
- {
- Str255 itemStr;
- FontInfo macFontInfo;
- Rect margin;
- Boolean savedAlloc;
-
- CAbstractText::IAbstractText(anEnclosure, aSupervisor,
- 1, 1, 0, 0, sizELASTIC, sizELASTIC,
- 432);
- wholeLines = 1;
-
- UseLongCoordinates(1);
- SetPort(macPort);
-
- savedAlloc = SetAllocation(kAllocCanFail);
- lineStarts = (long **) NewHandle(sizeof(long) * 1000);
- hText = NewHandle(0);
- frame_origin.h = frame.left;
- frame_origin.v = frame.top;
- teLength = 0L;
- txFont = monaco;
- txFace = 0;
- txMode = srcCopy;
- txSize = 9;
- tabWidth = 25;
- nLines = 1L;
- (*lineStarts)[0] = 0L;
- (*lineStarts)[1] = 0L;
- SetAllocation(savedAlloc);
- FailNIL((Handle)lineStarts);
- SetWholeLines(wholeLines);
- SetFontStuff();
- AdjustBounds();
- spacingCmd = 50L;
- alignCmd = 41L;
-
- FitToEnclosure(TRUE, TRUE);
-
- SetRect(&margin, 2, 2, -2, -2);
- ChangeSize(&margin, FALSE);
- }
-
- long GNUStaticPane::indexToLine(long selIndex)
- {
- register long i, delta;
-
- if (selIndex <= 0L || nLines <= 1L || teLength < 1L)
- return (0L);
- else if (selIndex >= teLength)
- return ((long) (nLines - 1L));
- else
- {
- i = (nLines) >> 1;
- delta = (nLines) >> 1;
- if (delta < 1L)
- delta = 1L;
- while (delta > 0L)
- {
- if (selIndex == (*lineStarts)[i])
- delta = 0L;
- else if (selIndex > (*lineStarts)[i])
- {
- if (selIndex < (*lineStarts)[i + 1])
- delta = 0L;
- else
- i += delta;
- } else
- i -= delta;
- if (delta)
- {
- delta >>= 1;
- if (delta < 1L)
- delta = 1L;
- }
- }
- }
- if (i < 0L)
- i = 0L;
- else if (i >= nLines)
- i = nLines - 1L;
- return ((long) i);
- }
-
- void GNUStaticPane::SetFontStuff(void)
- {
- register long i;
- long oldFont, oldFace, oldSize, oldMode;
- GrafPtr oldPort;
- FontInfo theFontInfo;
- short tot = 0,totwidth = 0;
-
- GetPort(&oldPort);
- SetPort(thePort);
- oldFont = (thePort)->txFont;
- oldFace = (thePort)->txFace;
- oldSize = (thePort)->txSize;
- oldMode = (thePort)->txMode;
- TextFont(txFont);
- TextFace(txFace);
- TextSize(txSize);
- TextMode(txMode);
- for (i = 0; i < 256; i++)
- {
- short width = CharWidth((unsigned char) i);
- theCharWidths[i] = width;
- if (width)
- {
- totwidth += width;
- tot++;
- }
- }
- GetFontInfo(&theFontInfo);
- tabWidth = totwidth*4/tot;
- lineHeight = theFontInfo.ascent + theFontInfo.descent + theFontInfo.leading;
- SetScales(theFontInfo.widMax, lineHeight);
- fontAscent = theFontInfo.ascent;
- TextFont(oldFont);
- TextFace(oldFace);
- TextSize(oldSize);
- TextMode(oldMode);
- SetPort(oldPort);
- }
-
- void GNUStaticPane::CalcLineStarts(void)
- {
- register unsigned char *charPtr;
- register long charCount;
- register short lineLength,maxLineLength;
- register unsigned char ch;
- long maxLineStarts;
- unsigned char *charBase;
- unsigned char *oldCharPtr;
- long oldCharCount, tempOffset;
- (*lineStarts)[0] = 0L;
- (*lineStarts)[1] = 0L;
- maxLineStarts = GetHandleSize((Handle) lineStarts) / (long) sizeof(long) - 2;
- lineLength = 0L;
- maxLineLength = 0;
- nLines = 0L;
- charBase = (unsigned char *) *(hText);
- charPtr = charBase;
- charCount = teLength;
- if (charCount > 0L)
- {
- while (charCount--)
- {
- ch = *charPtr++;
- lineLength++;
- if ((ch == '\r' || ch == '\n'))
- {
- if (nLines >= maxLineStarts)
- {
- tempOffset = charPtr - charBase;
- maxLineStarts = nLines+1000;
- SetHandleSize((Handle) lineStarts, sizeof(long) * (maxLineStarts+2));
- charBase = (unsigned char *) *(hText);
- charPtr = charBase + tempOffset;
- }
- (*lineStarts)[++nLines] = charPtr - charBase;
- if (lineLength > maxLineLength) maxLineLength = lineLength;
- lineLength = 0L;
- }
- }
- SetHandleSize((Handle) lineStarts, (long) sizeof(long) * (nLines + 3));
- (*lineStarts)[++nLines] = charPtr - charBase;
- }
- }
-
- void GNUStaticPane::Dispose(void)
- {
- if (hText)
- DisposHandle(hText);
- if (lineStarts)
- DisposHandle((Handle)lineStarts);
- inherited::Dispose();
- }
-
- void GNUStaticPane::Draw(Rect * area)
- {
- GrafPtr oldPort;
- register unsigned char *textPtr;
- register long firstLine, i, thisStart, nextStart;
- Point cursorPt;
- short oldFont, oldFace, oldSize, oldMode, height, gap;
- LongRect longInterior;
- Rect all;
-
- GetInterior(&longInterior);
- FrameToQDR(&longInterior, &all);
-
- GetPort(&oldPort);
- SetPort(thePort);
- firstLine = position.v+(area->top-all.top)/lineHeight;
- if (firstLine < 0) firstLine = 0;
- if (firstLine < nLines && teLength > 0L)
- {
- cursorPt.h = all.left;
- cursorPt.v = all.top+(firstLine-position.v+1)*lineHeight;
- oldFont = (thePort)->txFont;
- oldFace = (thePort)->txFace;
- oldSize = (thePort)->txSize;
- oldMode = (thePort)->txMode;
- TextFont(txFont);
- TextFace(txFace);
- TextSize(txSize);
- TextMode(txMode);
- HLock(hText);
- textPtr = (unsigned char *) *(hText);
- while (firstLine < nLines && cursorPt.v <= area->bottom)
- {
- thisStart = (*lineStarts)[firstLine];
- i = thisStart;
- nextStart = (*lineStarts)[firstLine + 1];
- if (nextStart > thisStart && (textPtr[nextStart - 1] == '\r' || textPtr[nextStart - 1] == '\n'))
- nextStart--;
- MoveTo(cursorPt.h, cursorPt.v);
- while (thisStart < nextStart)
- {
- while (i < nextStart && textPtr[i] != '\t')
- i++;
- if (i > thisStart)
- DrawText(&(textPtr[thisStart]), 0, (long) (i - thisStart));
- if (i < nextStart && textPtr[i] == '\t')
- {
- MoveTo(area->left + ((thePort->pnLoc.h - area->left + tabWidth) / tabWidth) * tabWidth, thePort->pnLoc.v);
- i++;
- }
- thisStart = i;
- if (thePort->pnLoc.h > area->right)
- thisStart = nextStart;
- }
- firstLine++;
- cursorPt.v += lineHeight;
- }
- HUnlock(hText);
- TextFont(oldFont);
- TextFace(oldFace);
- TextSize(oldSize);
- TextMode(oldMode);
- }
- SetPort(oldPort);
- ((CDocument *)itsSupervisor)->dirty = 0;
- }
-
- void GNUStaticPane::GetSteps(short *hStep, short *vStep)
- {
- short phStep, pvStep;
-
- CPanorama::GetSteps(&phStep, &pvStep);
- *hStep = phStep;
- *vStep = pvStep;
- }
-
- void GNUStaticPane::Scroll(
- long hDelta,
- long vDelta,
- Boolean redraw)
- {
- inherited::Scroll(hDelta, vDelta, redraw);
- }
-
- void GNUStaticPane::PrintPage(
- short pageNum,
- short pageWidth,
- short pageHeight,
- CPrinter * aPrinter)
- {
- inherited::PrintPage(pageNum, pageWidth, pageHeight, aPrinter);
- }
-
- void GNUStaticPane::SetTextPtr(
- Ptr textPtr,
- long textLength)
- {
- SetHandleSize(hText, textLength);
- BlockMove(textPtr, *hText, textLength);
- teLength = textLength;
- CalcLineStarts();
- SetFontStuff();
- AdjustBounds();
- Refresh();
- }
-
- Handle GNUStaticPane::GetTextHandle(void)
- {
- return hText;
- }
-
- void GNUStaticPane::SetFontNumber(short aFontNumber)
- {
- txFont = aFontNumber;
- SetFontStuff();
- SetSpacingCmd(spacingCmd);
- AdjustBounds();
- Refresh();
- RefreshBorder();
- SetWholeLines(wholeLines);
- Refresh();
- RefreshBorder();
- }
-
- void GNUStaticPane::SetFontStyle(short aStyle)
- {
- if (aStyle == 0)
- {
- txFace = 0;
- } else
- {
- txFace ^= aStyle;
- }
- SetFontStuff();
- Refresh();
- RefreshBorder();
- SetWholeLines(wholeLines);
- Refresh();
- RefreshBorder();
- }
-
- void GNUStaticPane::SetFontSize(short aSize)
- {
- txSize = aSize;
- SetFontStuff();
- SetSpacingCmd(spacingCmd);
- }
-
- void GNUStaticPane::SetTextMode(short aMode)
- {
- txMode = aMode;
- SetFontStuff();
- Refresh();
- }
-
- void GNUStaticPane::SetSpacingCmd(long aSpacingCmd)
- {
- FontInfo macFontInfo;
- long extra;
-
- spacingCmd = aSpacingCmd;
- SetFontStuff();
- switch (aSpacingCmd)
- {
- case 50L:
- extra = 0;
- break;
- case 51L:
- extra = lineHeight / 2;
- break;
- case 52L:
- extra = lineHeight;
- break;
- }
- lineHeight += extra;
- fontAscent += extra;
- Refresh();
- RefreshBorder();
- SetWholeLines(wholeLines);
- CalcAperture();
- AdjustBounds();
- Refresh();
- RefreshBorder();
- }
-
- long GNUStaticPane::GetSpacingCmd(void)
- {
- return spacingCmd;
- }
-
- long GNUStaticPane::GetAlignCmd(void)
- {
- return alignCmd;
- }
-
- void GNUStaticPane::GetGNUTEFontInfo(
- FontInfo * macFontInfo)
- {
- SetPort(macPort);
- ForceNextPrepare();
- TextFont(txFont);
- TextFace(txFace);
- TextSize(txSize);
- GetFontInfo(macFontInfo);
- }
-
- void GNUStaticPane::ResizeFrame(
- Rect * delta)
- {
- short hDelta;
- short vDelta;
- inherited::ResizeFrame(delta);
-
- frame_origin.h += delta->left;
- frame_origin.v += delta->top;
-
- hDelta = position.h * hScale - frame.left + frame_origin.h;
- vDelta = position.v * vScale - frame.top + frame_origin.v;
- frame.left += hDelta;
- frame.right += hDelta;
- frame.top += vDelta;
- hOrigin += hDelta;
- vOrigin += vDelta;
- SetFontStuff();
- AdjustBounds();
- Refresh();
- }
-
- void GNUStaticPane::AdjustBounds(void)
- {
- bounds.left = bounds.top = 0;
- bounds.bottom = GetHeight(0, nLines);
- bounds.right = (lineWidth - 1) / hScale + 1;
- if (itsScrollPane != ((void *) 0))
- {
- itsScrollPane->AdjustScrollMax();
- }
- }
-
- long GNUStaticPane::FindLine(
- long charPos)
- {
- long lineNum;
-
- if ((nLines == 0) || (charPos < (*lineStarts)[1]))
- {
- return (0);
- }
- if (charPos >= (*lineStarts)[nLines - 1])
- if (charPos == teLength)
- if (((char *) (*hText))[charPos - 1] == 13)
- return nLines;
- else
- return nLines - 1;
- lineNum = 1;
- while (charPos >= (*lineStarts)[lineNum + 1])
- {
- lineNum++;
- }
- return (lineNum);
- }
-
- long GNUStaticPane::GetLength(void)
- {
- return teLength;
- }
-
- void GNUStaticPane::GetTextStyle(short *whichAttributes, TextStyle * aStyle)
- {
- aStyle->tsFont = txFont;
- aStyle->tsFace = txFace;
- aStyle->tsSize = txSize;
- aStyle->tsColor.red = 1;
- aStyle->tsColor.green = 1;
- aStyle->tsColor.blue = 1;
- }
-
- void GNUStaticPane::GetCharStyle(long charOffset, TextStyle * aStyle)
- {
- aStyle->tsFont = txFont;
- aStyle->tsFace = txFace;
- aStyle->tsSize = txSize;
- aStyle->tsColor.red = 1;
- aStyle->tsColor.green = 1;
- aStyle->tsColor.blue = 1;
- }
-
- void GNUStaticPane::GetExtent(
- long *theHExtent,
- long *theVExtent)
- {
- LongRect longInterior;
-
- GetInterior(&longInterior);
- *theVExtent = nLines;
- *theHExtent = longInterior.right - longInterior.left;
- }
-
- long GNUStaticPane::GetHeight(long startLine, long endLine)
- {
- long height;
-
- if (endLine > nLines)
- {
- height = nLines - startLine + 1;
- if (((char *) *hText)[teLength - 1] == 13)
- ++height;
- } else
- height = endLine - startLine + 1;
- return height * lineHeight;
- }
-
- void GNUStaticPane::GetSelection(long *selStart, long *selEnd)
- {
- *selStart = 0;
- *selEnd = 0;
- }
-
- long GNUStaticPane::GetCharOffset( LongPt *aPt)
- {
- return 0L;
- }
-
- void GNUStaticPane::GetCharPoint( long offset, LongPt *aPt)
- {
- }
-
- void GNUStaticPane::TypeChar(char theChar, short theModifers)
- {
- }
-
- void GNUStaticPane::SetSelection( long selStart, long selEnd, Boolean fRedraw)
- {
- }
-
- Handle GNUStaticPane::CopyTextRange( long start, long end)
- {
- return NULL;
- }
-
- void GNUStaticPane::PerformEditCommand( long theCommand)
- {
- }
-
- void GNUStaticPane::InsertTextPtr( Ptr text, long length, Boolean fRedraw)
- {
- }
-